package cn.hd.userService.service.role.impl;

import cn.hd.commonService.entity.*;
import cn.hd.commonService.exception.CustomException;
import cn.hd.userService.mapper.role.SysRoleFuncMapper;
import cn.hd.userService.service.function.ISysFunctionService;
import cn.hd.userService.service.role.SysRoleFuncService;
import cn.hd.userService.service.role.SysRoleService;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * created by dsl on 2022/6/28
 */
@Service
public class SysRoleFuncServiceImpl extends ServiceImpl<SysRoleFuncMapper, SysRoleFunc> implements SysRoleFuncService {

    @Autowired
    private SysRoleFuncMapper sysRoleFuncMapper;

    @Autowired
    private SysRoleService roleService;
    @Autowired
    private ISysFunctionService functionService;

    /**
     * 根据功能获取角色和功能的关联关系
     * @CreatedBy dsl
     */
    public List<SysRoleFunc> getList(List<Integer> functionIdList){
        LambdaQueryWrapper<SysRoleFunc> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRoleFunc::getFunctionId,functionIdList);
        return sysRoleFuncMapper.selectList(wrapper);
    }

    public void saveRelation(String roleIds,String functionIds){
        //根据角色删除角色和功能的关联关系
        List<Integer> roleList = Convert.toList(Integer.class, roleIds);
        LambdaQueryWrapper<SysRoleFunc> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRoleFunc::getRoleId,roleList);
        sysRoleFuncMapper.delete(wrapper);
        if (!StrUtil.isEmptyIfStr(functionIds)){//功能信息不为空，进行功能信息的保存
            List<Integer> functionList = Convert.toList(Integer.class, functionIds);
            List<SysRoleFunc> result = new ArrayList<>();
            for (Integer roleId : roleList) {
                SysRole sysRole = roleService.getById(roleId);
                Optional.ofNullable(sysRole).orElseThrow(()->new CustomException("角色信息有误"));
                for (Integer functionId : functionList) {
                    SysFunction function = functionService.getById(functionId);
                    Optional.ofNullable(function).orElseThrow(()->new CustomException("功能信息有误"));
                    result.add(new SysRoleFunc(roleId,functionId));
                }
            }
            //批量保存
            saveBatch(result);
        }
    }

    /**
     * @CreatedBy dsl
     * 根据角色id获取角色和资源的关联关系
     */
    public List<SysRoleFunc> getListByRoleId(int roleId){
        LambdaQueryWrapper<SysRoleFunc> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleFunc::getRoleId,roleId);
       return sysRoleFuncMapper.selectList(wrapper);
    }

    @Override
    public void deleteByRoleId(List<Integer> roleId) {
        if (roleId != null && !roleId.isEmpty()){
            LambdaQueryWrapper<SysRoleFunc> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(SysRoleFunc::getRoleId,roleId);
            sysRoleFuncMapper.delete(wrapper);
        }

    }
}
